﻿using CallCenter.Data.Base;
using CallCenter.Data.Models;
using CallCenter.Data.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace CallCenter.Data.BizUtilities
{
   public class CreateUserRoleUtility :BaseUtility
    {
        public CreateUserRoleUtility(DataManager manager)
            : base(manager)
        { }
        public bool InsertUserRoles (RegisterViewModel registerUser)
        {
            registerUser.Password = CryptUtils.GetPasswordEncrypted(registerUser.Password);

            using (var command = this.Factory.GetCommand("usp_UserCrud", true, false))
            {

                command.AddParameter("@pMode", 0)
                    .AddParameter("@pUserName", registerUser.UserName)
                    .AddParameter("@pPassword", registerUser.Password)
                     .AddParameter("@pConfirmPassword", registerUser.Password)
                    .AddParameter("@pFullName", registerUser.FullName)
                    .AddParameter("@pEmail", registerUser.Email);
               


                var param = this.Factory.AddParameter(command, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                var rowsaffected = command.ExecuteNonQuery();

                if (rowsaffected > 0)
                {
                    var filePath = HostingEnvironment.MapPath("~/Content/Emails/NewUserRegistration.html");
                    var fileContent = File.ReadAllText(filePath);

                    EmailUtility.SendMail("New Account Registration",
                                    string.Format(fileContent, registerUser.FullName, registerUser.UserName),
                                    registerUser.Email);
                    //this.Bridge.RoleUser.InsertRoleUser(new RoleUserModel { RoleId = registerUser.RoleId, UserId = registerUser.Id });

                }
                return true;
            }

            return false;
        }

        //public ReturnMessage<CreateUserRolesVM> InsertCreateUserRole(CreateUserRolesVM model)
        //{
        //    var returnMessage = new ReturnMessage<CreateUserRolesVM>();
        //    //  if(this.ValidateScratchPadModel(model))
        //    //   {
        //    var id = this.Insert(model.code, model.description);
        //    if (id == -1)
        //    {
        //        returnMessage.Message = "We cannot insert data to database";
        //        returnMessage.Status = false;
        //    }
        //    else
        //    {
        //        model.id = id;
        //        returnMessage.Status = true;
        //    }
        //    return returnMessage;
        //}

        //public ReturnMessage<CreateUserRolesVM> UpdateUserRole(CreateUserRolesVM model)
        //{
        //    var returnMessage = new ReturnMessage<CreateUserRolesVM>();
        //    //  if(this.ValidateScratchPadModel(model))
        //    //   {
        //    var id = this.Update(model.code, model.description, model.id);
        //    if (id == -1)
        //    {
        //        returnMessage.Message = "We cannot insert data to database";
        //        returnMessage.Status = false;
        //    }
        //    else
        //    {
        //        model.id = id;
        //        returnMessage.Status = true;
        //    }
        //    return returnMessage;
        //}


        public int Insert(string UserName, string Password, string ConfirmPassword, int RoleId)
        {
            int nRtn = -1;
            string sql = "INSERT INTO tbl_Users (UserName , Password, ConfirmPassword,RoleId) Values(@UserName, @Password, @ConfirmPassword, @RoleId) SELECT @pResult =@@IDENTITY";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@UserName", SqlDbType.NVarChar, 10, UserName);
                this.Factory.AddParameter(cmd, "@Password", SqlDbType.NVarChar, 100, Password);

                this.Factory.AddParameter(cmd, "@ConfirmPassword", SqlDbType.NVarChar, 10, ConfirmPassword);
                this.Factory.AddParameter(cmd, "@RoleId", SqlDbType.NVarChar, 100, RoleId);


                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;
        }

        //public int Update(string UserName, string Password, string ConfirmPassword, int RoleId , int id)
        //{
        //    int nRtn = -1;
        //    string sql = "UPDATE tbl_Users SET code = @code, description = @desc WHERE id = @id";

        //    using (var cmd = this.Factory.GetCommand(sql))
        //    {
        //        this.Factory.AddParameter(cmd, "@UserName", SqlDbType.NVarChar, 10, UserName);
        //        this.Factory.AddParameter(cmd, "@Password", SqlDbType.NVarChar, 100, Password);

        //        this.Factory.AddParameter(cmd, "@ConfirmPassword", SqlDbType.NVarChar, 10, ConfirmPassword);
        //        this.Factory.AddParameter(cmd, "@RoleId", SqlDbType.NVarChar, 100, RoleId);


        //        var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
        //        param.Direction = System.Data.ParameterDirection.Output;

        //        this.Factory.ExecuteNonQuery(cmd);
        //        nRtn = Convert.ToInt32(param.Value);
        //    }
        //    return nRtn;
        //}


     


    }
}
